const express = require('express');
const multer = require('multer')
// const path = require('path')
const fs = require('fs')

const router = express.Router();

//图片上传
router.post(
  '/',
  multer({
    //设置文件存储路径
    dest: "public/imgs",
  }).array("file", 1),
  function (req, res, next) {
    let files = req.files;
    let file = files[0];
    let fileInfo = {};
    const filename= Date.now().toString() + "_" + file.originalname;
    let path = "public/imgs/" + filename;
    fs.renameSync("./public/imgs/" + file.filename, path);
    //获取文件基本信息
    fileInfo.type = file.mimetype;
    fileInfo.name = file.originalname;
    fileInfo.size = file.size;
    fileInfo.path = 'http://124.222.96.201:8190/imgs/'+filename;
    fileInfo.filename = file.filename;
    res.send({
      code: 200,
      msg: "OK",
      data: fileInfo,
    });
  }
)

module.exports=router
